﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace SFJDManagement
{
    /// <summary>
    /// UC_MultiCheckBox.xaml 的交互逻辑
    /// </summary>
    public partial class UC_MultiToggle : UserControl
    {
        private string[] buttonsName;
        private int fontSize;
        private Thickness buttonMargin;
        private Thickness buttonPadding;
        public delegate void CheckedChangeHandler(object sender, EventArgs e);
        public event CheckedChangeHandler CheckedChange;
        WrapPanel wrapPanel = new WrapPanel();

        /// <summary>
        /// 一个由给定的参数作为名称的多个切换开关组件
        /// </summary>
        /// <param name="parameter">用作按钮名称的string数组</param>
        public UC_MultiToggle(string[] parameter)
        {
            buttonsName = parameter;
            InitializeComponent();
            AddButtons();
        }

        public UC_MultiToggle(DataGrid dataGrid)
        {
            List<string> list = new List<string>(dataGrid.Columns.Count);
            foreach(DataGridColumn column in dataGrid.Columns)
            {
                list.Add(column.Header.ToString());
            }
            buttonsName = list.ToArray();
            InitializeComponent();
            AddButtons();
        }

        /// <summary>
        /// 一个由给定的参数作为名称的多个切换开关组件
        /// </summary>
        /// <param name="parameter">用作按钮名称的string数组</param>
        /// <param name="fontsize">按钮字号</param>
        /// <param name="margin">按钮外边距</param>
        /// <param name="padding">按钮内边距</param>
        public UC_MultiToggle(string[] parameter, int fontsize, Thickness margin, Thickness padding)
        {
            buttonsName = parameter;
            ButtonFontSize = fontsize;
            ButtonMargin = margin;
            ButtonPadding = padding;
            InitializeComponent();
            AddButtons();
        }

        private void AddButtons()
        {
            foreach (string name in buttonsName)
            {
                ToggleButton toggle = new ToggleButton();
                toggle.Margin = ButtonMargin;
                toggle.Padding = ButtonPadding;
                toggle.FontSize = ButtonFontSize;
                toggle.Content = name;
                toggle.IsChecked = true;
                toggle.Click += Toggle_Click;
                wrapPanel.Children.Add(toggle);
            }
            Content = wrapPanel;
        }

        private void Toggle_Click(object sender, RoutedEventArgs e)
        {
            //ToggleButton button = sender as ToggleButton;
            CheckedChange(sender, e);
        }

        /// <summary>
        /// 按钮显示文字的字号
        /// </summary>
        public int ButtonFontSize
        {
            set {
                fontSize = value;
                if (wrapPanel.Children.Count == 0)
                    return;
                foreach (Control c in wrapPanel.Children)
                {
                    c.FontSize = value;
                };
            }
            get { return fontSize < 1 ? 10 : fontSize; }
        }

        /// <summary>
        /// 按钮外边距
        /// </summary>
        public Thickness ButtonMargin
        {
            set {
                buttonMargin = value;
                if (wrapPanel.Children.Count == 0)
                    return;
                foreach (Control c in wrapPanel.Children)
                {
                    c.Margin = value;
                };
            }
            get { return buttonMargin == null ? new Thickness(10) : buttonMargin; }
        }

        /// <summary>
        /// 按钮内边距
        /// </summary>
        public Thickness ButtonPadding
        {
            set {
                buttonPadding = value;
                if (wrapPanel.Children.Count == 0)
                    return;
                foreach(Control c in wrapPanel.Children)
                {
                    c.Padding = value;
                };
            }
            get { return buttonPadding == null ? new Thickness(2) : buttonPadding; }
        }
    }
}
